package com.change.copy.utils;


import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.change.copy.dto.SiliconFlowDto;
import com.change.copy.vo.SiliconFlowVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 硅基流动 ai 工具类
 */
@Slf4j
@Service
public class SiliconFlowAiUtils extends AiUtil {
    @Value("${siliconFlow.api}")
    private String siliconFlowApi;

    @Value("${siliconFlow.password}")
    private String siliconFlowPassword;

    @Value("${siliconFlow.model}")
    private String siliconFlowModel;


    @Override
    public String getContent(String content) {
        try {
            String url = siliconFlowApi;
            Map<String, String> headers = new HashMap<>(16);
            headers.put("authorization", siliconFlowPassword);
            SiliconFlowDto siliconFlowDto = SiliconFlowDto.getDto(content)
                    .setModel(siliconFlowModel);
            String body = HttpUtil.createPost(url)
                    .addHeaders(headers)
                    .body(JSON.toJSONString(siliconFlowDto))
                    .execute()
                    .body();
            SiliconFlowVo siliconFlowVo = JSONObject.parseObject(body, SiliconFlowVo.class);
            return siliconFlowVo.getContent();
        } catch (HttpException e) {
            log.error("getContent 硅基流动API报错", e);
            throw e;
        }
    }

}
